<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "gky_patent".
 *
 * @property string $id
 * @property string $patentname
 * @property string $patentno
 * @property string $patenttype
 * @property string $patentdate
 * @property string $author
 * @property integer $sort
 * @property string $regno
 * @property string $checkunit
 * @property string $updatetime
 * @property integer $isfinish
 * @property string $addman
 * @property string $addmanid
 * @property string $addtime
 * @property string $mark
 */
class Patent extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_patent';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[  'patentno', 'regno','isfinish', 'addmanid','patentdate', 'updatetime', 'addtime'], 'safe'],
            [[ 'sort'], 'integer'],
            [['patentname', 'patenttype', 'author','checkunit', 'addman', 'mark'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'patentname' => '专利名称',
            'patentno' => '专利编号',
            'patenttype' => '专利类型',
            'patentdate' => '专利获得日期',
            'author' => '作者',
            'sort' => '排名',
            'regno' => '登记号',
            'checkunit' => '审核单位',
            'updatetime' => '更新时间',
            'isfinish' => '是否完成',
            'addman' => '添加人',
            'addmanid' => '添加人编号',
            'addtime' => '添加时间',
             'mark' => '备注',
        ];
    }
    
    
    
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            //添加修改人的信息 由于这部分没有必要自己填写 所以直接获取登录人的信息  在保存的时候设置就好
            $id = Yii::$app->user->identity->id;
            //添加人的id
            $this->addmanid = $id;
            //添加人的姓名
            $name = GkyUser::find()->where(['id' => $id])->one()->username;
            $this->addman = $name;
            //更新时间
            $time = date("Y-m-d");
            $this->updatetime = $time;
            //添加时间
            $this->addtime = $time;
            return true;
        } else {
            return false;
        }
    }
    
    
    /**
     * 查询专利列表，返回字段为专利名，作者，添加时间
     *
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getList($pageNow, $pageSize){

        $list = Patent::find()
            ->select(['id','patentname','author','addtime'])
            ->orderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
        return $list;
    }
    
}
